from json import encoder
import jwt

class Myjwt():
    def __init__(self):
        self.SECRET_KEY = 'asfsf!#$#$@$#^%#$sdfwtwa134342432323fffwf'

        # 生成jwt
    def jwt_encode(self,data):
            token = jwt.encode(data,self.SECRET_KEY,algorithm='HS256')
            return str(token,encoding='utf8')
        
        # 解密jwt
    def jwt_decode(self,token):
            try:
                data = jwt.decode(token,self.SECRET_KEY,algorithm='JS256')
                # 重新加密获取新的token
                newtoken = jwt.encode(data,self.SECRET_KEY,algorithm='HS256')
                return str(newtoken,encoding='utf8')
            except:
                return None

        # 对比
    def check(self,token,newtoken):
            if token == newtoken:
                return True
            else:
                return False
myjwt = Myjwt()
